var path = require('path'),
  webpack = require('webpack');
var ExtractTextPlugin = require("extract-text-webpack-plugin"),
  rucksack = require('rucksack-css'),
  autoprefixer = require('autoprefixer'),
  TransferWebpackPlugin = require('transfer-webpack-plugin');

var customTheme = require(path.join(__dirname, 'custom-theme.js'));

module.exports = {
  entry: {
    'bundle': [path.join(__dirname, 'src', 'entry.jsx')]
  },
  output: {
    path: path.join(__dirname, 'build'),
    filename: '[name].js',
    chunkFilename: '[id].chunk.js',
  },
  // @@@@@@@@@@@
  babel: {
    compact: false,
    cacheDirectory: true,
    presets: [
      require.resolve('babel-preset-es2015-ie'),
      require.resolve('babel-preset-react'),
      require.resolve('babel-preset-stage-0'),
    ],
    plugins: [
      require.resolve('babel-plugin-add-module-exports'),
      require.resolve('babel-plugin-transform-decorators-legacy'),
    ],
  },
  postcss: [
    rucksack(),
    autoprefixer({
      browsers: ['last 2 versions', 'Firefox ESR', '> 1%', 'ie >= 8'],
    }),
  ],
  node: {
    "child_process": "empty",
    "cluster": "empty",
    "dgram": "empty",
    "dns": "empty",
    "fs": "empty",
    "module": "empty",
    "net": "empty",
    "readline": "empty",
    "repl": "empty",
    "tls": "empty"
  },
  resolve: {
    root: [
      path.resolve(__dirname, 'src')
    ],
    alias: {
      // components is a copy of r1/components
      //antd: path.resolve(__dirname, 'components', 'antd'),
      //zipjs: path.resolve(__dirname, 'customized_modules', 'zipjs'),
      //'ic-file-loader': path.resolve(__dirname, 'customized_modules', 'ic-file-loader')
    },
    extensions: ['', '.js', '.jsx']
  },
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loaders: [
          'react-hot',
          'es3ify',    // Fix ie8 compatibility
          'babel'
        ]
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract([
          'css?sourceMap&-restructuring',
          'postcss'
        ])
      },
      {
        test: /\.less$/,
        loader: ExtractTextPlugin.extract([
          'css',
          'postcss',
          'less?{"modifyVars":' + JSON.stringify(customTheme) + '}'
        ])
      },
      {test: /\.woff(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=application/font-woff'},
      {test: /\.woff2(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=application/font-woff'},
      {test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=application/octet-stream'},
      {test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
      {test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&minetype=image/svg+xml'},
      {test: /\.(png|jpg|jpeg|gif|ico)(\?v=\d+\.\d+\.\d+)?$/i, loader: 'url?limit=10000'},
      {test: /\.json$/, loader: 'json'},
      {test: /\.html?$/, loader: 'file?name=[name].[ext]'}
    ]
  },
  plugins: [
    new webpack.DefinePlugin({
      process: {
        env: {
          NODE_ENV: JSON.stringify('production')
        }
      }
    }),
    new ExtractTextPlugin('[name].css', {
      disable: false,
      allChunks: true,
    }),
    new webpack.optimize.UglifyJsPlugin({
      compress: {
        warnings: false
      }
    }),
    new webpack.optimize.OccurenceOrderPlugin(),
    new webpack.optimize.DedupePlugin(),
    new webpack.NoErrorsPlugin(),
    // http://www.open-open.com/lib/view/open1450681593198.html
    //把指定文件夹下的文件复制到指定的目录
    new TransferWebpackPlugin(
      [
        {from: path.join(__dirname, 'htdocs')}
      ],
      path.join(__dirname, 'build')
    )
  ],
  // devtool: "source-map"
};